#include "https_shijian.h"
#include "main.h"
#include "https.h"


int https_get_ShiJian(void)
{
  int res = 0;
  char buf[50];

  res = HTTP_Get_ShiJian(&SYSTEM_State.RiQi_Data);
  if(res)
  {
    shijiani_DEBUG("获取时间失败\r\n");
    return -1;
  }
  return 0;
}

//获取时间与日期
int HTTP_Get_ShiJian(_RiQi_Data * read_buf)
{
  int return_dat=0;
  uint8_t res=0;
  char buf[50];
  char *str=NULL;
  int i=0,j=0;
  char *http_recv_buff=NULL;

  if(read_buf==NULL)
    return 1;

  http_recv_buff = (char*)os_malloc(1024);

  shijiani_DEBUG("开始获取时间\r\n");

  res = HTTP_Read_Data(HTTPS_SuNing_ShiJian,http_recv_buff,GET_REQ); 

  if(res==0)
  {
    //shijiani_DEBUG("获取到的数据:%s\r\n",http_recv_buff);
    
    str=strstr((char *)http_recv_buff, ":\""); 
    if(str!=NULL)
    {     

      str+=2;
      for(i=0;;i++)
      {
        if(str[i]==' ')
        {
          read_buf->riqi[i]='\0';
          break;
        }
        read_buf->riqi[i]=str[i];      
      }
      shijiani_DEBUG("解析到的日期:%s\r\n",read_buf->riqi);
      str+=i+1;     
      for(i=0;;i++)
      {
        if(str[i]=='"')
        {
          read_buf->shijian[i]='\0';
          break;
        }
        read_buf->shijian[i]=str[i];      
      }

      shijiani_DEBUG("解析到的时间:%s\r\n",read_buf->shijian);


      str = read_buf->riqi;
      
      j=0;
      for(i=0;str[i]!='\0';i++)
      {
        if(str[i]=='-')
        {
          
          break;
        }
        buf[i]=str[i];      
      }
      buf[i]='\0';
      read_buf->nian=atoi(buf);
      //shijiani_DEBUG("年:%d\r\n",read_buf->nian);
      j+=i+1;
      str+=i+1;
      for(i=0;str[i]!='\0';i++)
      {
        if(str[i]=='-')
        {
          
          break;
        }
        buf[i]=str[i];      
      }
      buf[i]='\0';
      read_buf->yue=atoi(buf); 
      //shijiani_DEBUG("月:%d\r\n",read_buf->yue);
      //shijiani_DEBUG("str1:%s j:%d\r\n",str,j);
   
      str+=i+1;
      //shijiani_DEBUG("str2:%s j:%d\r\n",str,j);
      for(i=0;str[i]!='\0';i++)
      {
        if(str[i]=='\0')
        {
          
          break;
        }
        buf[i]=str[i];      
      }
      buf[i]='\0';
      read_buf->ri=atoi(buf);
      //shijiani_DEBUG("日:%d\r\n",read_buf->ri);

      str = read_buf->shijian;
      j=0;
      for(i=0;str[i]!='\0';i++)
      {
        if(str[i]==':')
        {
          
          break;
        }
        buf[i]=str[i];      
      }
      buf[i]='\0';
      read_buf->shi=atoi(buf); 
      //shijiani_DEBUG("时:%d\r\n",read_buf->shi);

      j+=i+1;
      str+=i+1;
      for(i=0;str[i]!='\0';i++)
      {
        if(str[i]==':')
        {
          
          break;
        }
        buf[i]=str[i];      
      }
      buf[i]='\0';
      read_buf->fen=atoi(buf);  
      //shijiani_DEBUG("分:%d\r\n",read_buf->fen);
      //shijiani_DEBUG("str1:%s j:%d\r\n",str,j);
      j+=i+1;     
      str+=i+1;
      //shijiani_DEBUG("str2:%s j:%d\r\n",str,j);
      for(i=0;str[i]!='\0';i++)
      {
        if(str[i]=='\0')
        {
          
          break;
        }
        buf[i]=str[i];      
      }
      buf[i]='\0';
      read_buf->miao=atoi(buf);     
      //shijiani_DEBUG("秒:%d\r\n",read_buf->miao);   
    }else
    {
      shijiani_DEBUG("解析时间失败\r\n");
      return_dat= -1;
    }  
  }else
  {
    shijiani_DEBUG("获取时间失败:%d\r\n", res);
    return_dat= -2;
  }
  os_free(http_recv_buff);
  return return_dat;
}


